package com.iwop.moto.controller;

import com.iwop.moto.comm.ErrCode;
import com.iwop.moto.model.ResultModel;
import com.iwop.moto.model.SuperRecordModel;
import com.iwop.moto.service.AuditHistoryService;
import com.iwop.moto.service.IDataService;
import com.iwop.moto.service.MGoodsService;
import com.iwop.moto.utils.stringUtil.ObjectId;
import com.iwop.moto.utils.stringUtil.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * Created by mobom on 2016/10/21.
 */
@Controller
public class AuditGoodsController extends BaseController {
    @Autowired
    private AuditHistoryService auditHistoryService;
    @Autowired
    MGoodsService mgoodsService;

    /**
     * status[1:通过][2:拒绝]
     */
    @RequestMapping("/api/audit/goods/add")
    @ResponseBody
    public ResultModel auditGoodosAdd() {
        SuperRecordModel SRModel = this.getSRModel();
        try {
            String tid = ObjectId.UUID();
            String sid = SRModel.getString("sid");
            SRModel.put("sid", tid);
            SRModel.put("audit_type", "StoreGoods");
            SRModel.put("audit_fkey", SRModel.getString("sid"));
            SRModel.put("audit_user_id", SRModel.getString("session_id"));
            SRModel.put("audit_time", new Date());
            auditHistoryService.insert(SRModel);
            String audit_status = SRModel.getString("audit_status");
            SRModel.put("status", audit_status);
            SRModel.put("checkuserid", SRModel.getString("session_id"));
            SRModel.put("checktime", new Date());
            SRModel.put("checkreason", SRModel.getString("audit_describe"));
            if (audit_status.equals("1")) {
                SRModel.put("status_flag", "1");
            }
            SRModel.put("sid", sid);
            mgoodsService.upMGoodsCheckstatus(SRModel);
            return getSucceed(ErrCode.SUCCEED);
        } catch (Exception e) {
            e.printStackTrace();
            return getErr(ErrCode.ERROR_SYSTEM);
        }
    }

    /**
     * page 第几页
     * row 每页多少条
     */
    @RequestMapping("/api/audit/goods/list")
    @ResponseBody
    public ResultModel auditGoodosList() {
        try {
            SuperRecordModel SRModel = this.getSRModel();
            SRModel.put("checkstatus", "0");
            int page = StringUtils.getSafeInt(SRModel.getString("page"));
            int row = StringUtils.getSafeInt(SRModel.getString("row"));
            if (page < 1 || row < 1) {
                return getErr(ErrCode.ERROR_SYSTEM);
            }
            int begin = (page - 1) * row;
            SRModel.put("begin", begin);
            SRModel.put("end", row);
            List<SuperRecordModel> list = mgoodsService.findMGoodsListToAudit(SRModel);
            int countMGoods = mgoodsService.countMGoodsToCheck(SRModel);
            SuperRecordModel srm = new SuperRecordModel();
            srm.put("goods", list);
            srm.put("counts", countMGoods);
            return getSucceed(srm, ErrCode.SUCCEED);
        } catch (Exception e) {
            e.printStackTrace();
            return getErr(ErrCode.ERROR_SYSTEM);
        }
    }
}
